{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n",
        "# (beta) Channels Last Memory Format in PyTorch\n",
        "**Author**: [Vitaly Fedyunin](https://github.com/VitalyFedyunin)\n",
        "\n",
        "## What is Channels Last\n",
        "\n",
        "Channels last memory format is an alternative way of ordering NCHW tensors in memory preserving dimensions ordering. Channels last tensors ordered in such a way that channels become the densest dimension (aka storing images pixel-per-pixel).\n",
        "\n",
        "For example, classic (contiguous) storage of NCHW tensor (in our case it is two 4x4 images with 3 color channels) look like this:\n",
        "\n",
        ".. figure:: /_static/img/classic_memory_format.png\n",
        "   :alt: classic_memory_format\n",
        "\n",
        "Channels last memory format orders data differently:\n",
        "\n",
        ".. figure:: /_static/img/channels_last_memory_format.png\n",
        "   :alt: channels_last_memory_format\n",
        "\n",
        "Pytorch supports memory formats (and provides back compatibility with existing models including eager, JIT, and TorchScript) by utilizing  existing strides structure.\n",
        "For example, 10x3x16x16 batch in Channels last format will have strides equal to (768, 1, 48, 3).\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Channels last memory format is implemented for 4D NCHW Tensors only.\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Memory Format API\n",
        "\n",
        "Here is how to convert tensors between contiguous and channels\n",
        "last memory formats.\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Classic PyTorch contiguous tensor\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import torch\n",
        "\n",
        "N, C, H, W = 10, 3, 32, 32\n",
        "x = torch.empty(N, C, H, W)\n",
        "print(x.stride())  # Ouputs: (3072, 1024, 32, 1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Conversion operator\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x = x.to(memory_format=torch.channels_last)\n",
        "print(x.shape)  # Outputs: (10, 3, 32, 32) as dimensions order preserved\n",
        "print(x.stride())  # Outputs: (3072, 1, 96, 3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Back to contiguous\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x = x.to(memory_format=torch.contiguous_format)\n",
        "print(x.stride())  # Outputs: (3072, 1024, 32, 1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Alternative option\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x = x.contiguous(memory_format=torch.channels_last)\n",
        "print(x.stride())  # Ouputs: (3072, 1, 96, 3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Format checks\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(x.is_contiguous(memory_format=torch.channels_last))  # Ouputs: True"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "There are minor difference between the two APIs ``to`` and\n",
        "``contiguous``. We suggest to stick with ``to`` when explicitly\n",
        "converting memory format of tensor.\n",
        "\n",
        "For general cases the two APIs behave the same. However in special\n",
        "cases for a 4D tensor with size ``NCHW`` when either: ``C==1`` or\n",
        "``H==1 && W==1``, only ``to`` would generate a proper stride to\n",
        "represent channels last memory format.\n",
        "\n",
        "This is because in either of the two cases above, the memory format\n",
        "of a tensor is ambiguous, i.e. a contiguous tensor with size\n",
        "``N1HW`` is both ``contiguous`` and channels last in memory storage.\n",
        "Therefore, they are already considered as ``is_contiguous``\n",
        "for the given memory format and hence ``contiguous`` call becomes a\n",
        "no-op and would not update the stride. On the contrary, ``to``\n",
        "would restride tensor with a meaningful stride on dimensions whose\n",
        "sizes are 1 in order to properly represent the intended memory\n",
        "format\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "special_x = torch.empty(4, 1, 4, 4)\n",
        "print(special_x.is_contiguous(memory_format=torch.channels_last))  # Ouputs: True\n",
        "print(special_x.is_contiguous(memory_format=torch.contiguous_format))  # Ouputs: True"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Same thing applies to explicit permutation API ``permute``. In\n",
        "special case where ambiguity could occur, ``permute`` does not\n",
        "guarantee to produce a stride that properly carry the intended\n",
        "memory format. We suggest to use ``to`` with explicit memory format\n",
        "to avoid unintended behavior.\n",
        "\n",
        "And a side note that in the extreme case, where three non-batch\n",
        "dimensions are all equal to ``1`` (``C==1 && H==1 && W==1``),\n",
        "current implementation cannot mark a tensor as channels last memory\n",
        "format.\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create as channels last\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x = torch.empty(N, C, H, W, memory_format=torch.channels_last)\n",
        "print(x.stride())  # Ouputs: (3072, 1, 96, 3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "``clone`` preserves memory format\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "y = x.clone()\n",
        "print(y.stride())  # Ouputs: (3072, 1, 96, 3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "``to``, ``cuda``, ``float`` ... preserves memory format\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "if torch.cuda.is_available():\n",
        "    y = x.cuda()\n",
        "    print(y.stride())  # Ouputs: (3072, 1, 96, 3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "``empty_like``, ``*_like`` operators preserves memory format\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "y = torch.empty_like(x)\n",
        "print(y.stride())  # Ouputs: (3072, 1, 96, 3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Pointwise operators preserves memory format\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "z = x + y\n",
        "print(z.stride())  # Ouputs: (3072, 1, 96, 3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Conv, Batchnorm modules using cudnn backends support channels last\n",
        "(only works for CudNN >= 7.6). Convolution modules, unlike binary\n",
        "p-wise operator, have channels last as the dominating memory format.\n",
        "IFF all inputs are in contiguous memory format, the operator\n",
        "produces output in contiguous memory format. Otherwise, output wil\n",
        "be in channels last memroy format.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "if torch.backends.cudnn.version() >= 7603:\n",
        "    model = torch.nn.Conv2d(8, 4, 3).cuda().half()\n",
        "    model = model.to(memory_format=torch.channels_last)  # Module parameters need to be channels last\n",
        "\n",
        "    input = torch.randint(1, 10, (2, 8, 4, 4), dtype=torch.float32, requires_grad=True)\n",
        "    input = input.to(device=\"cuda\", memory_format=torch.channels_last, dtype=torch.float16)\n",
        "\n",
        "    out = model(input)\n",
        "    print(out.is_contiguous(memory_format=torch.channels_last))  # Ouputs: True"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "When input tensor reaches a operator without channels last support,\n",
        "a permutation should automatically apply in the kernel to restore\n",
        "contiguous on input tensor. This introduces overhead and stops the\n",
        "channels last memory format propagation. Nevertheless, it guarantees\n",
        "correct output.\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Performance Gains\n",
        "Channels last memory format optimizations are available on both GPU and CPU.\n",
        "On GPU, the most significant performance gains are observed on NVidia's\n",
        "hardware with Tensor Cores support running on reduced precision\n",
        "(``torch.float16``).\n",
        "We were able to archive over 22% perf gains with channels last\n",
        "comparing to contiguous format, both while utilizing\n",
        "'AMP (Automated Mixed Precision)' training scripts.\n",
        "Our scripts uses AMP supplied by NVidia\n",
        "https://github.com/NVIDIA/apex.\n",
        "\n",
        "``python main_amp.py -a resnet50 --b 200 --workers 16 --opt-level O2  ./data``\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# opt_level = O2\n",
        "# keep_batchnorm_fp32 = None <class 'NoneType'>\n",
        "# loss_scale = None <class 'NoneType'>\n",
        "# CUDNN VERSION: 7603\n",
        "# => creating model 'resnet50'\n",
        "# Selected optimization level O2:  FP16 training with FP32 batchnorm and FP32 master weights.\n",
        "# Defaults for this optimization level are:\n",
        "# enabled                : True\n",
        "# opt_level              : O2\n",
        "# cast_model_type        : torch.float16\n",
        "# patch_torch_functions  : False\n",
        "# keep_batchnorm_fp32    : True\n",
        "# master_weights         : True\n",
        "# loss_scale             : dynamic\n",
        "# Processing user overrides (additional kwargs that are not None)...\n",
        "# After processing overrides, optimization options are:\n",
        "# enabled                : True\n",
        "# opt_level              : O2\n",
        "# cast_model_type        : torch.float16\n",
        "# patch_torch_functions  : False\n",
        "# keep_batchnorm_fp32    : True\n",
        "# master_weights         : True\n",
        "# loss_scale             : dynamic\n",
        "# Epoch: [0][10/125] Time 0.866 (0.866) Speed 230.949 (230.949) Loss 0.6735125184 (0.6735) Prec@1 61.000 (61.000) Prec@5 100.000 (100.000)\n",
        "# Epoch: [0][20/125] Time 0.259 (0.562) Speed 773.481 (355.693) Loss 0.6968704462 (0.6852) Prec@1 55.000 (58.000) Prec@5 100.000 (100.000)\n",
        "# Epoch: [0][30/125] Time 0.258 (0.461) Speed 775.089 (433.965) Loss 0.7877287269 (0.7194) Prec@1 51.500 (55.833) Prec@5 100.000 (100.000)\n",
        "# Epoch: [0][40/125] Time 0.259 (0.410) Speed 771.710 (487.281) Loss 0.8285319805 (0.7467) Prec@1 48.500 (54.000) Prec@5 100.000 (100.000)\n",
        "# Epoch: [0][50/125] Time 0.260 (0.380) Speed 770.090 (525.908) Loss 0.7370464802 (0.7447) Prec@1 56.500 (54.500) Prec@5 100.000 (100.000)\n",
        "# Epoch: [0][60/125] Time 0.258 (0.360) Speed 775.623 (555.728) Loss 0.7592862844 (0.7472) Prec@1 51.000 (53.917) Prec@5 100.000 (100.000)\n",
        "# Epoch: [0][70/125] Time 0.258 (0.345) Speed 774.746 (579.115) Loss 1.9698858261 (0.9218) Prec@1 49.500 (53.286) Prec@5 100.000 (100.000)\n",
        "# Epoch: [0][80/125] Time 0.260 (0.335) Speed 770.324 (597.659) Loss 2.2505953312 (1.0879) Prec@1 50.500 (52.938) Prec@5 100.000 (100.000)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Passing ``--channels-last true`` allows running a model in Channels last format with observed 22% perf gain.\n",
        "\n",
        "``python main_amp.py -a resnet50 --b 200 --workers 16 --opt-level O2 --channels-last true ./data``\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# opt_level = O2\n",
        "# keep_batchnorm_fp32 = None <class 'NoneType'>\n",
        "# loss_scale = None <class 'NoneType'>\n",
        "#\n",
        "# CUDNN VERSION: 7603\n",
        "#\n",
        "# => creating model 'resnet50'\n",
        "# Selected optimization level O2:  FP16 training with FP32 batchnorm and FP32 master weights.\n",
        "#\n",
        "# Defaults for this optimization level are:\n",
        "# enabled                : True\n",
        "# opt_level              : O2\n",
        "# cast_model_type        : torch.float16\n",
        "# patch_torch_functions  : False\n",
        "# keep_batchnorm_fp32    : True\n",
        "# master_weights         : True\n",
        "# loss_scale             : dynamic\n",
        "# Processing user overrides (additional kwargs that are not None)...\n",
        "# After processing overrides, optimization options are:\n",
        "# enabled                : True\n",
        "# opt_level              : O2\n",
        "# cast_model_type        : torch.float16\n",
        "# patch_torch_functions  : False\n",
        "# keep_batchnorm_fp32    : True\n",
        "# master_weights         : True\n",
        "# loss_scale             : dynamic\n",
        "#\n",
        "# Epoch: [0][10/125] Time 0.767 (0.767) Speed 260.785 (260.785) Loss 0.7579724789 (0.7580) Prec@1 53.500 (53.500) Prec@5 100.000 (100.000)\n",
        "# Epoch: [0][20/125] Time 0.198 (0.482) Speed 1012.135 (414.716) Loss 0.7007197738 (0.7293) Prec@1 49.000 (51.250) Prec@5 100.000 (100.000)\n",
        "# Epoch: [0][30/125] Time 0.198 (0.387) Speed 1010.977 (516.198) Loss 0.7113101482 (0.7233) Prec@1 55.500 (52.667) Prec@5 100.000 (100.000)\n",
        "# Epoch: [0][40/125] Time 0.197 (0.340) Speed 1013.023 (588.333) Loss 0.8943189979 (0.7661) Prec@1 54.000 (53.000) Prec@5 100.000 (100.000)\n",
        "# Epoch: [0][50/125] Time 0.198 (0.312) Speed 1010.541 (641.977) Loss 1.7113249302 (0.9551) Prec@1 51.000 (52.600) Prec@5 100.000 (100.000)\n",
        "# Epoch: [0][60/125] Time 0.198 (0.293) Speed 1011.163 (683.574) Loss 5.8537774086 (1.7716) Prec@1 50.500 (52.250) Prec@5 100.000 (100.000)\n",
        "# Epoch: [0][70/125] Time 0.198 (0.279) Speed 1011.453 (716.767) Loss 5.7595844269 (2.3413) Prec@1 46.500 (51.429) Prec@5 100.000 (100.000)\n",
        "# Epoch: [0][80/125] Time 0.198 (0.269) Speed 1011.827 (743.883) Loss 2.8196096420 (2.4011) Prec@1 47.500 (50.938) Prec@5 100.000 (100.000)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The following list of models has the full support of Channels last and showing 8%-35% perf gains on Volta devices:\n",
        "``alexnet``, ``mnasnet0_5``, ``mnasnet0_75``, ``mnasnet1_0``, ``mnasnet1_3``, ``mobilenet_v2``, ``resnet101``, ``resnet152``, ``resnet18``, ``resnet34``, ``resnet50``, ``resnext50_32x4d``, ``shufflenet_v2_x0_5``, ``shufflenet_v2_x1_0``, ``shufflenet_v2_x1_5``, ``shufflenet_v2_x2_0``, ``squeezenet1_0``, ``squeezenet1_1``, ``vgg11``, ``vgg11_bn``, ``vgg13``, ``vgg13_bn``, ``vgg16``, ``vgg16_bn``, ``vgg19``, ``vgg19_bn``, ``wide_resnet101_2``, ``wide_resnet50_2``\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The following list of models has the full support of Channels last and showing 26%-76% perf gains on Intel(R) Xeon(R) Ice Lake (or newer) CPUs:\n",
        "``alexnet``, ``densenet121``, ``densenet161``, ``densenet169``, ``googlenet``, ``inception_v3``, ``mnasnet0_5``, ``mnasnet1_0``, ``resnet101``, ``resnet152``, ``resnet18``, ``resnet34``, ``resnet50``, ``resnext101_32x8d``, ``resnext50_32x4d``, ``shufflenet_v2_x0_5``, ``shufflenet_v2_x1_0``, ``squeezenet1_0``, ``squeezenet1_1``, ``vgg11``, ``vgg11_bn``, ``vgg13``, ``vgg13_bn``, ``vgg16``, ``vgg16_bn``, ``vgg19``, ``vgg19_bn``, ``wide_resnet101_2``, ``wide_resnet50_2``\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Converting existing models\n",
        "\n",
        "Channels last support is not limited by existing models, as any\n",
        "model can be converted to channels last and propagate format through\n",
        "the graph as soon as input (or certain weight) is formatted\n",
        "correctly.\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Need to be done once, after model initialization (or load)\n",
        "model = model.to(memory_format=torch.channels_last)  # Replace with your model\n",
        "\n",
        "# Need to be done for every input\n",
        "input = input.to(memory_format=torch.channels_last)  # Replace with your input\n",
        "output = model(input)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "However, not all operators fully converted to support channels last\n",
        "(usually returning contiguous output instead). In the example posted\n",
        "above, layers that does not support channels last will stop the\n",
        "memory format propagation. In spite of that, as we have converted the\n",
        "model to channels last format, that means each convolution layer,\n",
        "which has its 4 dimensional weight in channels last memory format,\n",
        "will restore channels last memory format and benefit from faster\n",
        "kernels.\n",
        "\n",
        "But operators that does not support channels last does introduce\n",
        "overhead by permutation. Optionally, you can investigate and identify\n",
        "operators in your model that does not support channels last, if you\n",
        "want to improve the performance of converted model.\n",
        "\n",
        "That means you need to verify the list of used operators\n",
        "against supported operators list https://github.com/pytorch/pytorch/wiki/Operators-with-Channels-Last-support,\n",
        "or introduce memory format checks into eager execution mode and run your model.\n",
        "\n",
        "After running the code below, operators will raise an exception if the output of the\n",
        "operator doesn't match the memory format of the input.\n",
        "\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def contains_cl(args):\n",
        "    for t in args:\n",
        "        if isinstance(t, torch.Tensor):\n",
        "            if t.is_contiguous(memory_format=torch.channels_last) and not t.is_contiguous():\n",
        "                return True\n",
        "        elif isinstance(t, list) or isinstance(t, tuple):\n",
        "            if contains_cl(list(t)):\n",
        "                return True\n",
        "    return False\n",
        "\n",
        "\n",
        "def print_inputs(args, indent=\"\"):\n",
        "    for t in args:\n",
        "        if isinstance(t, torch.Tensor):\n",
        "            print(indent, t.stride(), t.shape, t.device, t.dtype)\n",
        "        elif isinstance(t, list) or isinstance(t, tuple):\n",
        "            print(indent, type(t))\n",
        "            print_inputs(list(t), indent=indent + \"    \")\n",
        "        else:\n",
        "            print(indent, t)\n",
        "\n",
        "\n",
        "def check_wrapper(fn):\n",
        "    name = fn.__name__\n",
        "\n",
        "    def check_cl(*args, **kwargs):\n",
        "        was_cl = contains_cl(args)\n",
        "        try:\n",
        "            result = fn(*args, **kwargs)\n",
        "        except Exception as e:\n",
        "            print(\"`{}` inputs are:\".format(name))\n",
        "            print_inputs(args)\n",
        "            print(\"-------------------\")\n",
        "            raise e\n",
        "        failed = False\n",
        "        if was_cl:\n",
        "            if isinstance(result, torch.Tensor):\n",
        "                if result.dim() == 4 and not result.is_contiguous(memory_format=torch.channels_last):\n",
        "                    print(\n",
        "                        \"`{}` got channels_last input, but output is not channels_last:\".format(name),\n",
        "                        result.shape,\n",
        "                        result.stride(),\n",
        "                        result.device,\n",
        "                        result.dtype,\n",
        "                    )\n",
        "                    failed = True\n",
        "        if failed and True:\n",
        "            print(\"`{}` inputs are:\".format(name))\n",
        "            print_inputs(args)\n",
        "            raise Exception(\"Operator `{}` lost channels_last property\".format(name))\n",
        "        return result\n",
        "\n",
        "    return check_cl\n",
        "\n",
        "\n",
        "old_attrs = dict()\n",
        "\n",
        "\n",
        "def attribute(m):\n",
        "    old_attrs[m] = dict()\n",
        "    for i in dir(m):\n",
        "        e = getattr(m, i)\n",
        "        exclude_functions = [\"is_cuda\", \"has_names\", \"numel\", \"stride\", \"Tensor\", \"is_contiguous\", \"__class__\"]\n",
        "        if i not in exclude_functions and not i.startswith(\"_\") and \"__call__\" in dir(e):\n",
        "            try:\n",
        "                old_attrs[m][i] = e\n",
        "                setattr(m, i, check_wrapper(e))\n",
        "            except Exception as e:\n",
        "                print(i)\n",
        "                print(e)\n",
        "\n",
        "\n",
        "attribute(torch.Tensor)\n",
        "attribute(torch.nn.functional)\n",
        "attribute(torch)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "If you found an operator that doesn't support channels last tensors\n",
        "and you want to contribute, feel free to use following developers\n",
        "guide https://github.com/pytorch/pytorch/wiki/Writing-memory-format-aware-operators.\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Code below is to recover the attributes of torch.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "for (m, attrs) in old_attrs.items():\n",
        "    for (k, v) in attrs.items():\n",
        "        setattr(m, k, v)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Work to do\n",
        "There are still many things to do, such as:\n",
        "\n",
        "- Resolving ambiguity of N1HW and NC11 Tensors;\n",
        "- Testing of Distributed Training support;\n",
        "- Improving operators coverage.\n",
        "\n",
        "If you have feedback and/or suggestions for improvement, please let us\n",
        "know by creating [an issue](https://github.com/pytorch/pytorch/issues).\n",
        "\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)]"
    },
    "vscode": {
      "interpreter": {
        "hash": "cf18841ace8313d0bc088ca146c17a6c0040e82121d5cb75c0ea07172309253d"
      }
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
